<?php
/**
 * 學期編班
 * @author cik
 * @version $Id: SemesterController.php 93 2010-05-02 03:33:24Z yingkuang.chen@gmail.com $
 */
class StudentPlace_SemesterController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // 目前學年學期
        $year = $this->view->Semester['year'];
        $semester = $this->view->Semester['semester'];
        //查詢已編班人數
        $q = Doctrine_Query::create()
        ->select('a.sn,a.grade,COUNT(a.sn) AS cc')
        ->from('Student_Model_StudentSemester a')
        ->leftJoin('a.Student_Model_Student b')
        ->where('a.year=?', $year)
        ->andWhere('a.semester=?', $semester)
        ->andWhereIN('b.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING))
        ->groupBy('grade');
        $arr = array();
        foreach ($q->fetchArray() as $row)
        $arr[$row['grade']] = $row['cc'];
        $this->view->data = $arr;
    }

    /**
     * 參照上學期升級
     * @return unknown_type
     */
    function upgradeAction()
    {
        // 升級年級
        $grade = (int) $this->_getParam('grade');
        // 本學年
        $year = $this->view->Semester['year'];
        // 本學期
        $semester = $this->view->Semester['semester'];
        // 先檢查本學期班級是否設定
        $semesterClass = new Semester_Model_SemesterClass();
        $tr = Zend_Registry::get('Zend_Translate');

        $classArr = $semesterClass->getClassSetNum($year, $semester);
        if ($classArr[$grade] ==0)
        $this->_helper->json(array('result'=>false,
        'message'=>$tr->translate('not set class at current semester')));


//        if ($this->_getParam('redo') != 1) {
//            // 先檢查本學期是否有資料
//            $res = Doctrine_Query::create()
//            ->select('sn,COUNT(*) AS cc')
//            ->from('Student_Model_StudentSemester')
//            ->where('year=?', $year)
//            ->andWhere('semester=?', $semester)
//            ->andWhere('grade=?', $grade)
//            ->fetchOne();
//            // 有資料時回傳 false
//            if ($res->cc > 0)
//            $this->_helper->json(array('result'=>false,'message'=>$tr->translate('has semester data')));
//        }

        // 找上學期學期資料
        if ($semester==1) {
            $pervious_year = $year-1;
            $pervious_semester = 2;
            $pervious_grade = $grade -1;
        }
        else {
            $pervious_year = $year;
            $pervious_semester = 1;
            $pervious_grade = $grade;
        }

        // 找出上一學期資料
        $q = Doctrine_Query::create()
        ->select('a.*')
        ->from('Student_Model_StudentSemester a')
        ->leftJoin('a.Student_Model_Student b')
        ->where('a.year=?', $pervious_year)
        ->andWhere('a.semester=?', $pervious_semester)
        ->andWhere('a.grade=?', $pervious_grade)
        ->andWhereIN('b.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,
        Student_Model_StudentTransfer::HOMESCHOOLING));
        $res = $q->fetchArray();

        // 回傳找不到上學期資料
        if (count($res) == 0)
        $this->_helper->json(array('result'=>false,'message'=> $this->view->translate('none semester data')));

        // 重新升級先刪除舊記錄
        if ($this->_getParam('redo') == 1) {
            Doctrine_query::create()
            ->delete('Student_Model_StudentSemester')
            ->where('year=?', $year)
            ->andWhere('semester=?', $semester)
            ->andWhere('grade=?', $grade)
            ->execute();
        }

        // 新增至本學期
        foreach($res as $row) {
            $semesterStudent = new Student_Model_StudentSemester();
            unset($row['sn']);
            $row['year'] = $year;
            $row['semester'] = $semester;
            $row['grade'] = $grade;
            $semesterStudent->fromArray($row);
            $semesterStudent->save();
        }
        // 回傳成功訊息
        $this->_helper->json(array('result'=>true,'message'=>'success'));
    }


}

